﻿#include "CSqlOper.h"
#include <qdatetime.h>
CSqlOper::CSqlOper()
{
}

CSqlOper::~CSqlOper()
{
}

QVector<QVector<QString>> selPointInfo(QString& sErr)
{
	QVector<QVector<QString>>vecRedata;
	QString sGuid = "";
	CDatabaseExecData cData;
	cData.sTableName = "palarm_state";
	cData.sSql = "select * from palarm_state";
	cData.vecField.clear();
	cData.vecValue.clear();
	bool bRet = CPublicData::instance().m_pSqlDllOper->exec(cData);
	sErr = cData.sErr;
	QVector<QSqlRecord> vecDataInfo = cData.vecRecord;
	if (vecDataInfo.size() > 0)
	{
		for (int i = 0; i < vecDataInfo.size(); i++)
		{
			QVector<QString>vecData;
			vecData.push_back(vecDataInfo[i].value("GUID").toString().trimmed());
			vecData.push_back(QString::number(vecDataInfo[i].value("ALARM_TYPE").toInt()));
			vecData.push_back(vecDataInfo[i].value("NODE_GUID").toString().trimmed());
			vecData.push_back(vecDataInfo[i].value("NODE_NAME").toString().trimmed());
			vecData.push_back(QString::number(vecDataInfo[i].value("DEV_TYPE").toInt()));
			vecData.push_back(vecDataInfo[i].value("DEV_GUID").toString().trimmed());
			vecData.push_back(vecDataInfo[i].value("DEV_NAME").toString().trimmed());
			vecData.push_back(vecDataInfo[i].value("POINT_GUID").toString().trimmed());
			vecData.push_back(vecDataInfo[i].value("POINT_NAME").toString().trimmed());
			vecData.push_back(vecDataInfo[i].value("ALARM_STATE").toString().trimmed());
			vecData.push_back(vecDataInfo[i].value("ALARM_VALUE").toString().trimmed());
			vecData.push_back(vecDataInfo[i].value("ALARM_DESC").toString().trimmed());
			vecData.push_back(vecDataInfo[i].value("ALARM_TIME").toDateTime().toString("yyyy/MM/dd hh:mm:ss"));
			vecRedata.push_back(vecData);
		}
	}
	return vecRedata;
}

QVector<QVector<QString>> selPointInfoByType(int nType, QString& sErr)
{
	QVector<QVector<QString>>vecRedata;
	QString sGuid = "";
	CDatabaseExecData cData;
	cData.sTableName = "palarm_state";
	cData.sSql = "select * from palarm_state where DEV_TYPE=?";
	cData.vecField.clear();
	cData.vecValue.clear();
	cData.vecField.push_back("DEV_TYPE");
	cData.vecValue.push_back(QString::number(nType));
	bool bRet = CPublicData::instance().m_pSqlDllOper->exec(cData);
	sErr = cData.sErr;
	QVector<QSqlRecord> vecDataInfo = cData.vecRecord;
	if (vecDataInfo.size() > 0)
	{
		for (int i = 0; i < vecDataInfo.size(); i++)
		{
			QVector<QString>vecData;
			vecData.push_back(vecDataInfo[i].value("GUID").toString().trimmed());
			vecData.push_back(QString::number(vecDataInfo[i].value("ALARM_TYPE").toInt()));
			vecData.push_back(vecDataInfo[i].value("NODE_GUID").toString().trimmed());
			vecData.push_back(vecDataInfo[i].value("NODE_NAME").toString().trimmed());
			vecData.push_back(QString::number(vecDataInfo[i].value("DEV_TYPE").toInt()));
			vecData.push_back(vecDataInfo[i].value("DEV_GUID").toString().trimmed());
			vecData.push_back(vecDataInfo[i].value("DEV_NAME").toString().trimmed());
			vecData.push_back(vecDataInfo[i].value("POINT_GUID").toString().trimmed());
			vecData.push_back(vecDataInfo[i].value("POINT_NAME").toString().trimmed());
			vecData.push_back(vecDataInfo[i].value("ALARM_STATE").toString().trimmed());
			vecData.push_back(vecDataInfo[i].value("ALARM_VALUE").toString().trimmed());
			vecData.push_back(vecDataInfo[i].value("ALARM_DESC").toString().trimmed());
			vecData.push_back(vecDataInfo[i].value("ALARM_TIME").toDateTime().toString("yyyy/MM/dd hh:mm:ss"));
			vecRedata.push_back(vecData);
		}
	}
	return vecRedata;
}

bool insertPointInfo(QVector<QString>vecData, QString& sErr)
{
	CDatabaseExecData cData;
	cData.sTableName = "palarm_state";
	cData.sSql = "INSERT INTO palarm_state (GUID,ALARM_TYPE,NODE_GUID,NODE_NAME,DEV_TYPE,DEV_GUID,DEV_NAME,POINT_GUID,POINT_NAME,ALARM_STATE,ALARM_VALUE,ALARM_DESC,ALARM_TIME,DETEC_IMG_URL,DATA_HH,DATA_H,DATA_L,DATA_LL,UNIT)"
		"values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
	cData.addValue("GUID", vecData[0]);
	cData.addValue("ALARM_TYPE", vecData[1]);
	cData.addValue("NODE_GUID", vecData[2]);
	cData.addValue("NODE_NAME", vecData[3]);
	cData.addValue("DEV_TYPE", vecData[4]);
	cData.addValue("DEV_GUID", vecData[5]);
	cData.addValue("DEV_NAME", vecData[6]);
	cData.addValue("POINT_GUID", vecData[7]);
	cData.addValue("POINT_NAME", vecData[8]);
	cData.addValue("ALARM_STATE", vecData[9]);
	cData.addValue("ALARM_VALUE", vecData[10]);
	cData.addValue("ALARM_DESC", vecData[11]);
	cData.addValue("ALARM_TIME", vecData[12]);
	cData.addValue("DETEC_IMG_URL", vecData[13]);
	cData.addValue("DATA_HH", vecData[14]);
	cData.addValue("DATA_H", vecData[15]);
	cData.addValue("DATA_L", vecData[16]);
	cData.addValue("DATA_LL", vecData[17]);
	cData.addValue("UNIT", vecData[18]);
	return CPublicData::instance().m_pSqlDllOper->exec(cData);
}

bool updataPointInfo(QVector<QString>vecData, QString& sErr)
{
	bool bResult = false;
	CDatabaseExecData cData;
	cData.sTableName = "palarm_state";
	cData.sSql = "UPDATE palarm_state SET ALARM_TYPE=?,NODE_GUID=?,NODE_NAME=?,DEV_TYPE=?,DEV_GUID=?,DEV_NAME=?,POINT_GUID=?,POINT_NAME=?,ALARM_STATE=?,ALARM_VALUE=?,ALARM_DESC=?,ALARM_TIME=?"
		" WHERE GUID=?";
	cData.addValue("ALARM_TYPE", vecData[1]);
	cData.addValue("NODE_GUID", vecData[2]);
	cData.addValue("NODE_NAME", vecData[3]);
	cData.addValue("DEV_TYPE", vecData[4]);
	cData.addValue("DEV_GUID", vecData[5]);
	cData.addValue("DEV_NAME", vecData[6]);
	cData.addValue("POINT_GUID", vecData[7]);
	cData.addValue("POINT_NAME", vecData[8]);
	cData.addValue("ALARM_STATE", vecData[9]);
	cData.addValue("ALARM_VALUE", vecData[10]);
	cData.addValue("ALARM_DESC", vecData[11]);
	cData.addValue("ALARM_TIME", vecData[12]);
	cData.addValue("GUID", vecData[0]);
	bool bRet = CPublicData::instance().m_pSqlDllOper->exec(cData);
	return bResult;
}

bool updataPointInfo(CAlarmPoint* Point, QString& sErr)
{
	bool bResult = false;
	CDatabaseExecData cData;
	cData.sTableName = "palarm_state";
	cData.sSql = "UPDATE palarm_state SET ALARM_STATE=?,ALARM_VALUE=?,ALARM_DESC=?"
		" WHERE NODE_GUID=? and DEV_GUID=? and POINT_GUID=?";
	cData.addValue("ALARM_STATE", QString::number((int)Point->getAlarmConfirmStatus()));
	cData.addValue("ALARM_VALUE", QString::number(Point->getValue().toDouble()));
	cData.addValue("ALARM_DESC", "");
	cData.addValue("NODE_GUID", Point->getParent()->getParent()->getId());
	cData.addValue("DEV_GUID", Point->getParent()->getId());
	cData.addValue("POINT_GUID", Point->getId());
	bool bRet = CPublicData::instance().m_pSqlDllOper->exec(cData);
	return bResult;
}
